package com.luojun.servlets;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.luojun.entity.Commodity;
import com.luojun.entity.Page;
import com.luojun.service.UCommodityService;
import com.sun.beans.editors.EnumEditor;

/**
 * Servlet implementation class ToIndex
 */
@WebServlet("/ToIndex")
public class ToIndex extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ToIndex() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		Enumeration<String> headerNames =  request.getHeaderNames();
//		while(headerNames.hasMoreElements()){
//			String element = headerNames.nextElement();
//			String values = request.getHeader(element);
//		
//		}
		String name = request.getParameter("search");
		String agent = request.getHeader("User-Agent");
		HttpSession session = request.getSession();
		if(agent.indexOf("Mobile")!=-1){
			session.setAttribute("Type", 1);//是手机的
		}else{
			session.setAttribute("Type", 2);//不是手机的
		}
		String nickname = (String)session.getAttribute("nickname");
		if(nickname!=null){
			request.setAttribute("nickname", nickname);
			request.setAttribute("address", "ToPersonalCenter");
			request.setAttribute("zhuche", "");
			request.setAttribute("address2", "");
		}else{
			request.setAttribute("nickname", "登录");
			request.setAttribute("address", "ToLogin");
			request.setAttribute("zhuche", "注册");
			request.setAttribute("address2", "ToRegister");
		}
		
		
		String pageNum=request.getParameter("pageNum");
		int num=0;
		if(pageNum==null||pageNum.equals("")){
			num=1;
		}else{
			num=Integer.parseInt(pageNum);
		}
		if(name!=null&&name!=""){
			//调用业务逻辑层对象，处理结果实现跳转
			UCommodityService ucs = new UCommodityService();
			int count = ucs.findCountbyName(name);
			Page<Commodity> p = new Page<Commodity>(num,20);
			p.setAllCount(count);
			List<Commodity> list = ucs.findByPageName(p.getNowPageNum(), p.getPageSize(),name);
			p.setList(list);
			request.setAttribute("page",p);
		}else{
			//调用业务逻辑层对象，处理结果实现跳转
			UCommodityService ucs = new UCommodityService();
			int count = ucs.findCountByPage();
			Page<Commodity> p = new Page<Commodity>(num,20);
			p.setAllCount(count);
			List<Commodity> list = ucs.findByPage(p.getNowPageNum(), p.getPageSize());
			p.setList(list);
			request.setAttribute("page",p);
		}
		
		request.getRequestDispatcher("WEB-INF/jsp/indexs.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
